package com.incar.chuxing.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.incar.chuxing.R;
import com.incar.chuxing.adapter.ChooseCouponAdapter;
import com.incar.chuxing.api.ApiStore;
import com.incar.chuxing.api.UrlConstant;
import com.incar.chuxing.base.BaseActivity;
import com.incar.chuxing.base.BaseRcQuickAdapter;
import com.incar.chuxing.bean.SettlementBikeDetailBean;
import com.incar.chuxing.bean.SettlementDetailBean;
import com.incar.chuxing.utils.Constants;
import com.incar.chuxing.utils.IntentKey;
import com.incar.chuxing.utils.MyApplication;
import com.incar.chuxing.utils.StatusBarUtil;
import com.incar.chuxing.utils.Toast.ToastCompat;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ChooseCouponActivity extends BaseActivity {

    @BindView(R.id.recyclerView_choose_coupon)
    RecyclerView recyclerViewChooseCoupon;

    private Context mContext = this;
    private ChooseCouponAdapter carAdapter;
    private String orderId;
    private String bikeId;
    private String tripMode;
    @BindView(R.id.title_more)
    ImageView titleMore;
    @BindView(R.id.title_tv_more)
    TextView titleTvMore;
    @BindView(R.id.title_name)
    TextView titleName;
    @Override
    public int getLayoutId() {
        return R.layout.activity_choose_coupon;
    }

    @Override
    public void initData() {
        super.initData();

        Intent intent = getIntent();
        tripMode = intent.getStringExtra(IntentKey.INTENT_TRIP_MODE);
        if (tripMode.equals("1")) {
            orderId = intent.getStringExtra(IntentKey.INTENT_ORDER_ID);
            getSettlementDetail();
            final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
            recyclerViewChooseCoupon.setLayoutManager(linearLayoutManager);
            recyclerViewChooseCoupon.setItemAnimator(new DefaultItemAnimator());
            carAdapter = new ChooseCouponAdapter(mContext, R.layout.item_my_coupon);
            recyclerViewChooseCoupon.setAdapter(carAdapter);
            carAdapter.setOnItemClickListener(new BaseRcQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    Intent intent = new Intent();
                    intent.putExtra("couponPrice", carAdapter.getItem(position).getDenomination() + "");
                    intent.putExtra("couponId", carAdapter.getItem(position).getCouponId() + "");
                    intent.putExtra(IntentKey.INTENT_WEBVIEW_TITLE, carAdapter.getItem(position).getTitle() + "");
                    setResult(Constants.RESULT_CHOOSE_COUPON, intent);
                    finish();
                }
            });
        }
    }


    @Override
    public void initView() {

        titleName.setText("使用优惠券");
        titleMore.setVisibility(View.GONE);
        titleTvMore.setVisibility(View.VISIBLE);
        titleTvMore.setText("暂不使用");
    }

    private void getSettlementDetail() {

        ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).settlementDetail(((MyApplication) getApplication()).getRequestFieldMap(), orderId).enqueue(new Callback<SettlementDetailBean>() {
            @Override
            public void onResponse(Call<SettlementDetailBean> call, Response<SettlementDetailBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if (response.body().getStatus() == 200) {
                        SettlementDetailBean.AttachmentBean result = response.body().getAttachment();
                        carAdapter.replaceAll(result.getCoupons());
                    } else {
                        ToastCompat.makeText(mContext, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                    }
                } else {
                    ToastCompat.makeText(mContext, getResources().getString(R.string.serverError), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<SettlementDetailBean> call, Throwable t) {
                ToastCompat.makeText(mContext, getResources().getString(R.string.badNetwork), Toast.LENGTH_SHORT).show();
            }
        });
    }


    @OnClick({R.id.title_back, R.id.button_choose_coupon_no,R.id.title_tv_more})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                finish();
                break;
            case R.id.title_tv_more:
                Intent intent = new Intent();
                intent.putExtra("couponPrice", 0 + "");
                intent.putExtra("couponId", -1 + "");
                setResult(Constants.RESULT_CHOOSE_COUPON, intent);
                finish();
                break;
            case R.id.button_choose_coupon_no:
              /*  Intent intent = new Intent();
                intent.putExtra("couponPrice", 0 + "");
                intent.putExtra("couponId", -1 + "");
                setResult(Constants.RESULT_CHOOSE_COUPON, intent);
                finish();*/
                break;
        }
    }
}
